package com.hfut.jpoa.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.exception.DaoException;
import com.hfut.jpoa.dao.CustomerDAO;
import com.hfut.jpoa.dto.CustomerDTO;
import com.hfut.jpoa.model.Customer;
import com.ibatis.sqlmap.client.SqlMapClient;

public class CustomerDAOImpl implements CustomerDAO
{
	private SqlMapClient sqlMapClient;
	private static final Logger LOGGER = LoggerFactory
	        .getLogger(CustomerDAOImpl.class);

	public int deleteByPrimaryKey(Integer id) throws DaoException
	{
		Customer key = new Customer();
		key.setId(id);
		int rows = 0;
		try
		{
			rows = sqlMapClient.delete(
			        "tb_jpoa_customer.ibatorgenerated_deleteByPrimaryKey", key);
		} catch (SQLException e)
		{
			LOGGER.error("tb_jpoa_customer.ibatorgenerated_deleteByPrimaryKey",
			        e);
			throw new DaoException(
			        "tb_jpoa_customer.ibatorgenerated_deleteByPrimaryKey", e);
		}
		return rows;
	}

	public void insert(Customer record) throws DaoException
	{
		try
		{
			sqlMapClient.insert("tb_jpoa_customer.ibatorgenerated_insert",
			        record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Integer insertSelective(Customer record) throws DaoException
	{
		Integer num = null;
		try
		{
			num = (Integer) sqlMapClient.insert(
			        "tb_jpoa_customer.ibatorgenerated_insertSelective", record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	public Customer selectByPrimaryKey(Integer id) throws DaoException
	{
		Customer key = new Customer();
		key.setId(id);
		Customer record = null;
		try
		{
			record = (Customer) sqlMapClient.queryForObject(
			        "tb_jpoa_customer.ibatorgenerated_selectByPrimaryKey", key);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return record;
	}

	public int updateByPrimaryKeySelective(Customer record) throws DaoException
	{
		int rows = 0;
		try
		{
			rows = sqlMapClient
			        .update("tb_jpoa_customer.ibatorgenerated_updateByPrimaryKeySelective",
			                record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	public int updateByPrimaryKey(Customer record) throws DaoException
	{
		int rows = 0;
		try
		{
			rows = sqlMapClient.update(
			        "tb_jpoa_customer.ibatorgenerated_updateByPrimaryKey",
			        record);
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	public List<Customer> queryCustomers(Customer record) throws DaoException
	{
		List<Customer> result = null;
		try
		{
			result = sqlMapClient.queryForList(
			        "tb_jpoa_customer.ibatorgenerated_selectByPrimaryKey",
			        record);
			result = (result == null ? new ArrayList<Customer>() : result);
		} catch (SQLException e)
		{
			LOGGER.error("ibatorgenerated_selectByPrimaryKey", e);
			throw new DaoException("ibatorgenerated_selectByPrimaryKey", e);
		}
		return result;
	}

	public SqlMapClient getSqlMapClient()
	{
		return sqlMapClient;
	}

	public void setSqlMapClient(SqlMapClient sqlMapClient)
	{
		this.sqlMapClient = sqlMapClient;
	}

	public ResultDto<Customer> findInfoForPage(CustomerDTO dto)
	        throws DaoException
	{
		ResultDto<Customer> result = new ResultDto<Customer>();
		try
		{
			List<Customer> configsList = sqlMapClient.queryForList(
			        "tb_jpoa_customer.find_for_page", dto);
			result.setResult(configsList != null ? configsList
			        : new ArrayList<Customer>());
			BeanUtils.copyProperties(dto.getPagination(),
			        result.getPagination());
			result.getPagination().setTotal(
			        (Integer) sqlMapClient.queryForObject(
			                "tb_jpoa_customer.find_for_page_count", dto));
		} catch (SQLException e)
		{
			LOGGER.error("findInfoForPage exception", e);
			throw new DaoException("findInfoForPage exception", e);
		}
		return result;
	}
}